// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: leon, junxiao

#include <gflags/gflags.h>

#include "coin2/strategy/dex_strat/simple_arb_strategy.h"
#include "coin2/strategy/dex_strat/simple_amm_strategy.h"

DEFINE_bool(dry_run, false, "dry run");
DEFINE_string(driver, "", "driver config");
DEFINE_string(config, "", "strategy config");
DEFINE_int32(exit_after_min, 10080, "");

int main(int argc, char* argv[]) {
  google::InitGoogleLogging(argv[0]);
  gflags::ParseCommandLineFlags(&argc, &argv, true);

  auto path = std::experimental::filesystem::path(FLAGS_driver);
  coin2::app::DriverConfig app_config;
  app_config.MergeFrom(coin2::base::config::DriverConfigFromJson(path));
  LOG(INFO) << "app_config: " << app_config.DebugString();

  std::unique_ptr<coin2::strategy::IStrategy> strategy = nullptr;
  if (app_config.strategy().strategy_group() == "simple_arb") {
    strategy.reset(new coin2::strategy::dex_strat::SimpleArbStrategy(
        FLAGS_config,
        FLAGS_dry_run,
        std::optional<int64_t>()));
  } else if (app_config.strategy().strategy_group() == "simple_amm") {
    strategy.reset(new coin2::strategy::dex_strat::SimpleAmmStrategy(
        FLAGS_config,
        FLAGS_dry_run,
        std::optional<int64_t>()));
  } else {
    LOG(ERROR) << "Unknown strategy group!" << app_config.DebugString();
    return 1;
  }
  coin2::app::Driver driver(app_config, strategy.get());
  driver.RegisterCleanupHandler();
  driver.RegisterCleanupTimeout(FLAGS_exit_after_min * 60);
  driver.Run();
  return 0;
}
